package com.mdz.sbgl.Controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.mdz.sbgl.Config.ConfigJDBC;
import com.mdz.sbgl.Entity.Sjkmc;
import com.mdz.sbgl.Mapper.SjkmcMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import java.sql.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2020-12-15
 */
@Controller
@RequestMapping("/sbgl/sjkmc")
public class SjkmcController {

    @Autowired
    private SjkmcMapper sjkmcMapper;
    @Autowired
    private DruidDataSource dataSource;

    @ResponseBody
    @RequestMapping(value = "/getsjkmc", method = RequestMethod.GET)
    public List<Sjkmc> getsjkmc() {
        List<Sjkmc> sjkmcList = sjkmcMapper.selectList(null);
        return sjkmcList;
    }

    @ResponseBody
    @GetMapping("/getconn")
    public String getconn(){
        ConfigJDBC jdbc=new ConfigJDBC();
        String login_name = "1";
        String user_name = "1";
        String sql = "select * from sbuser where login_name=?" + " and user_name=?";
        jdbc.setDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        jdbc.setUrl("jdbc:sqlserver://localhost;SelectMethod=cursor;DatabaseName=sbgl");
        jdbc.setUsername("sa");
        jdbc.setPassword("45168537");
        System.out.println(jdbc.getDriver());
        System.out.println(jdbc.getUrl());
        System.out.println(jdbc.getUsername());
        System.out.println(jdbc.getPassword());
        dataSource.setDriverClassName(jdbc.getDriver());
        dataSource.setUrl(jdbc.getUrl());
        dataSource.setUsername(jdbc.getUsername());
        dataSource.setPassword(jdbc.getPassword());
        Connection connection=null;
        PreparedStatement preparedStatement  = null;
        ResultSet resultSet = null;
        try {
            connection=dataSource.getConnection(jdbc.getUsername(),jdbc.getPassword());
//            Connection connection=dataSource.getDataSource().getConnection(jdbc.getUsername(),jdbc.getPassword());
            System.out.println(connection);
            preparedStatement =connection.prepareStatement(sql);
            preparedStatement.setString(1, login_name);
            preparedStatement.setString(2, user_name);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                System.out.println("登录成功");
                return "成功";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "失败";
    }
}

